package com.ayanami.userservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.ayanami.userservice.entity.TbUser;
import com.ayanami.userservice.listener.DataEasyExcelListener;
import com.ayanami.userservice.mapper.TbUserMapper;
import com.ayanami.userservice.service.TbUserService;
import com.ayanami.userservice.vo.TbUserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author ayanami
 * @since 2020-07-20
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {
    @Override
    public IPage<TbUser> findUserAndDept(Page<TbUser> page,QueryWrapper<TbUser> queryWrapper) {
        return this.baseMapper.findUserAndDept(page,queryWrapper);
    }

    @Override
    public void exportUserInfoExcel(HttpServletResponse response,TbUserVO tbUserVO) throws IOException {
        QueryWrapper<TbUser> queryWrapper =new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tbUserVO.getUsername())) {
            queryWrapper.like("username", tbUserVO.getUsername());
        }
        if(tbUserVO.getDepartmentId()!=null) {
            queryWrapper.eq("department_id", tbUserVO.getDepartmentId());
        }
        if(StringUtils.isNotEmpty(tbUserVO.getEmail())) {
            queryWrapper.like("email", tbUserVO.getEmail());
        }
        if(StringUtils.isNotEmpty(tbUserVO.getNickname())) {
            queryWrapper.like("nickname", tbUserVO.getNickname());
        }
        if(tbUserVO.getSex()!=null){
            queryWrapper.eq("sex",tbUserVO.getSex());
        }
        List<TbUser> list = this.baseMapper.selectList(queryWrapper);
        String exportFileName = "TbUserInfo.xlsx";
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.addHeader("Content-Disposition", "attachment;filename=" + exportFileName);
        EasyExcel.write(response.getOutputStream(), TbUser.class)
                .sheet("用户信息")
                //自动列宽
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .doWrite(list);
    }

    @Override
    public void importUserInfoExcel(MultipartFile file) throws Exception {
        DataEasyExcelListener<TbUser> listener = new DataEasyExcelListener<>();
        InputStream inputStream = file.getInputStream();
        EasyExcel.read(inputStream, TbUser.class, listener).sheet(0).doRead();
        List<TbUser> data = listener.getData();
        System.out.println("导入的学生信息：");
        System.out.println(data);
        data.forEach(TbUser->{
            this.baseMapper.insert(TbUser);
        });
    }
}
